<?
	session_start();
	include('conn.php');
	$opt = $_REQUEST["q"];
	switch($opt)
	{
		case '1':
			$qry1 = "SELECT sc.stbtocustid, sc.CustId, sc.stbid, sc.ActivationDate, sc.TerminationDate, sc.RateperMonth, st.stbno, ct.cardno, c.CustomerName 
			from STBtoCustomer sc, stbTable st, cardTable ct, Customer c
			where sc.stbid = st.id and sc.cardid = ct.id and sc.custid = c.CustId and sc.status='1' order by stbtocustid desc";
			$qry = mysql_query($qry1);
			$data = array();

			while($rows = mysql_fetch_array($qry))
			{
				$data[] = array
					(			
						"id" => $rows['stbtocustid'],
						"custid" => $rows['CustId'],
						"custname" => $rows['CustomerName'],
						"stbid" => $rows['stbid'],
						"stbno" => $rows['stbno'],
						"cardno" => $rows['cardno'],
						"actdt" => $rows['ActivationDate'],
						"termdt" => $rows['TerminationDate'],
						"rpm" => $rows['RateperMonth']
					);
			}
			echo json_encode($data);
			break;
		
		case '2':
			$data = array();			
			$q = "select pairid from STBDetails where Active='1' and pairid not in (select pairid from STBtoCustomer where TerminationDate is null)";
			$r = mysql_query($q);
			while($rows = mysql_fetch_array($r)){
				$q2 = "select s.stbno,c.cardno from stbTable s,cardTable c where s.id = (select stbid from STBDetails where pairid='".$rows['pairid']."') and c.id =(select cardid from STBDetails where pairid='".$rows['pairid']."') ";
				$r2 = mysql_fetch_array(mysql_query($q2));
				$data[] = array
					(			
						"stbno" => $r2['stbno'],
						"cardno" => $r2['cardno'],						
					);					
			}
			echo json_encode($data);
			break;				
			
		case '3':
			$qry1 = "SELECT CustomerName,CustId from Customer where status='1' order by CustomerName";
			$qry = mysql_query($qry1);			
			while($rows = mysql_fetch_array($qry))
			{
				$data[] = array
				(			
					"cname" => $rows['CustomerName'],
					"cid" => $rows['CustId']
				);
			}
			echo json_encode($data);
			break;
		
		case '4':
			$stbno = $_REQUEST['stbno'];
			$qry1 = "SELECT cardno from cardTable where  status = '0' and id in(select cardid from STBDetails where stbid in(select id from stbTable where stbno ='$stbno'))";
			$qry = mysql_query($qry1);			
			while($rows = mysql_fetch_array($qry))
			{
				$data = $rows['cardno']	;					
			}
			echo ($data);
			break;
			
		case '5':
			$cardno = $_REQUEST['cardno'];
			$qry1 = "SELECT stbno from stbTable where status = '0' and id in(select stbid from STBDetails where cardid in(select id from cardTable where cardno ='$cardno'))";
			$qry = mysql_query($qry1);			
			while($rows = mysql_fetch_array($qry))
			{
				$data = $rows['stbno']	;					
			}
			echo ($data);
			break;
			
		default:
			echo "No option selected";
	}	
?>